package com.vrsys.aspect;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.vrsys.view.PageQuery;

@Component
@Aspect
public class PagingAdvice {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Modify paging index after searching in database.
     * @param  pageQuery  object returned by service paging method
     * @return void
     * @see
     */
    @AfterReturning(pointcut = "within(com.vrsys.service.*) && execution(* paging*(..))", returning = "pageQuery")
    public void modifyPageIndex(PageQuery<? extends Object> pageQuery) {
         logger.info("paging advice...");
        if (null != pageQuery.getData() && 0 < pageQuery.getData().size())
            pageQuery.setFromIndex(pageQuery.getFromIndex() + pageQuery.getData().size());
    }

    @Before(value="within(com.vrsys.service.*) && execution(* paging*(..))")
    public void testA() {
        logger.error("testA..");
    }
//
//    @After(value="within(com.vrsys.service.*) && execution(* paging*(..))")
//    public void testB() {
//        logger.error("paging advice...");
//        if(0 < pageQuery.getData().size())
//            pageQuery.setFromIndex(pageQuery.getFromIndex() + pageQuery.getElementNumber());
//    }
}
